package com.sec.android.app.myfiles.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;

public final class UnsupportedNameDialogFragment extends DialogFragment {

    public interface UnsupportedNameDialogCallback {
        void onUnsupportedFileCancel();
        void onUnsupportedFileSkip();
        void onUnsupportedFileRename(boolean applyAllItems);
    }

    private FileRecord mCurRecord;
    private UnsupportedNameDialogCallback mCallback;
    private boolean mSingleFile;

    public static UnsupportedNameDialogFragment getInstance(FileRecord record, UnsupportedNameDialogCallback callback, boolean isSingleFile) {
        UnsupportedNameDialogFragment fg = new UnsupportedNameDialogFragment();
        fg.mCurRecord = record;
        fg.mCallback = callback;
        fg.mSingleFile = isSingleFile;

        return fg;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        final Activity a = getActivity();

        AlertDialog.Builder builder = new AlertDialog.Builder(a);
        View dialogView = a.getLayoutInflater().inflate(R.layout.dialog_text_checkbox_common, null);
        builder.setView(dialogView);
        TextView tv = (TextView) dialogView.findViewById(R.id.dialog_main_text);

        int titleResId, tvResId;
        if (mCurRecord.isDirectory()) {
            titleResId = R.string.menu_rename_folder_title;
            tvResId = R.string.folder_name_not_allowed;
        } else {
            titleResId = R.string.menu_rename_file_title;
            tvResId = R.string.file_name_not_allowed;
        }

        builder.setTitle(titleResId);
        tv.setText(String.format(getString(tvResId), mCurRecord.getName()));

        final CheckBox applyAllItems = (CheckBox) dialogView.findViewById(R.id.dialog_checkbox);
        applyAllItems.setText(R.string.apply_to_all_items);
        if(AppFeatures.isEnableButtonBackgrounds(a.getApplicationContext())) {
            applyAllItems.setBackgroundResource(R.drawable.path_indicator_background);
        }
        // TODO : check sound
        applyAllItems.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // for click sound.
                SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.RENAME_SPECIAL_CHARACTER_POPUP, SamsungAnalyticsLog.Event.APPLY_TO_ALL_ITEMS, null);
            }
        });

        builder.setPositiveButton(getString(R.string.menu_rename), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                if (mCallback != null) {
                    mCallback.onUnsupportedFileRename(applyAllItems.isChecked());
                    SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.RENAME_SPECIAL_CHARACTER_POPUP, SamsungAnalyticsLog.Event.RENAME, null);
                }
            }
        });

        if (mSingleFile) {
            builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) {
                    if (mCallback != null) {
                        mCallback.onUnsupportedFileCancel();
                        SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.RENAME_SPECIAL_CHARACTER_POPUP, SamsungAnalyticsLog.Event.CANCEL, null);
                    }
                }
            });
        } else {
            builder.setNegativeButton(getString(R.string.skip), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) {
                    if (mCallback != null) {
                        mCallback.onUnsupportedFileSkip();
                        SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.RENAME_SPECIAL_CHARACTER_POPUP, SamsungAnalyticsLog.Event.SKIP, null);
                    }
                }
            });

            builder.setNeutralButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) {
                    if (mCallback != null) {
                        mCallback.onUnsupportedFileCancel();
                        SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.RENAME_SPECIAL_CHARACTER_POPUP, SamsungAnalyticsLog.Event.CANCEL, null);
                    }
                }
            });
        }

        builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return keyCode == KeyEvent.KEYCODE_MENU;
            }
        });
        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        if (mCallback != null) {
            mCallback.onUnsupportedFileCancel();
        }
        super.onCancel(dialog);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        // Be careful, onDismiss will call back from android framework after
        // screen orientation things.
        super.onDismiss(dialog);
    }

    public void showAllowingStateLoss(final FragmentManager fm, final String tag) {
        if (fm != null && !fm.isDestroyed()) {
            final FragmentTransaction t = fm.beginTransaction();
            t.add(this, tag);
            t.commitAllowingStateLoss();
        }
    }
}
